/*
 * Thread.cpp
 *
 *  Created on: 2010-10-17
 *      Author: tan
 */

#include "thread.h"

namespace light {

	Thread::Thread(Task task) {
		this->task = task;
	}
	Thread::Thread(TaskSimple task) {
		this->simpleTask = task;
		this->task = Thread::do_default_simple_task;
	}

    void Thread::suspend() throw(LightException)
    {
    }

    void Thread::yeald() throw(LightException)
    {
    }

    void Thread::terminate() throw(LightException)
    {
    }

    void Thread::start(void* param) throw(LightException)
    {
    	pthread_create(&threadId, NULL, task, param);
    }
    void Thread::start() throw(LightException)
    {
    	this->start(this);
    }

    void Thread::sleep(int64_t time) throw(LightException)
    {
    	usleep(time*1000);
    }

    void Thread::join() throw(LightException)
    {
    	pthread_join(threadId, NULL);
    }

    int32_t Thread::getId() throw()
    {
    	return (int32_t)threadId;
    }

	Thread::~Thread() {
		//
	}

	void* Thread::do_default_simple_task(void* param)
	{
		Thread* target = (Thread*)param;
		return target->simpleTask();
	}
}
